package com.smt.core.servlet;

import com.smt.core.controller.ControllerClass;
import com.smt.core.exceptions.NoImplementsControllerClass;
import com.smt.core.properties.PropertiesReader;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * User: Jose
 * Date: 10/05/12
 * Time: 21:40
 * To change this template use File | Settings | File Templates.
 */
public class SMTServlet  extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
    {
       //Procesaremos las peticiones de respuesta para ello usaremos inyeccion de depencias

        try {
            if (request!=null)
            {
                SMTServletConfig.getServletConfig().setRequest(request);
                if(PropertiesReader.get_asynchronusServlet())
                {
                    asynchronusResponse(request, response);
                }else{
                    //Tratamos la respuesta
                   Class cbClass = Class.forName("clase");
                   Class[]vectorClass = cbClass.getInterfaces();
                  if( Arrays.asList(vectorClass).contains(ControllerClass.class.getName()))
                  {
                      //Contiene nuestra interfaz
                       cbClass.newInstance();
                       Class parameter = request.getClass();
                       cbClass.getMethod("setRequest",parameter).invoke(cbClass,request);
                  }else{
                      throw new NoImplementsControllerClass();
                  }



                }
            } else {

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NoImplementsControllerClass noImplementsControllerClass) {
            noImplementsControllerClass.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InstantiationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (NoSuchMethodException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvocationTargetException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            response.sendError(404);
        }
    }

    /*
    Si es asincrona la repuesta usaremos el metodo asincrono
     */
    private void asynchronusResponse(HttpServletRequest request,HttpServletResponse response)
    {
        AsyncContext aCtx = request.startAsync(request, response);
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
    }
}
